1   /*
2    * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  
25  /*
26   *
27   *
28   * Used by UnicodeTest.sh.
29   *
30   * This class creates Java source files using Unicode characters
31   * that test the limits of what's possible
32   * - in situations where the platform encoding imposes limits
33   *   (command line arguments, non-Unicode file system)
34   * - in situations where full Unicode is supported
35   *   (file system access in UTF-8 locales and on Windows 2000++,
36   *    jar file contents)
37   *
38   * @author Norbert Lindenberg
39   */
40  
41  
42  
43  import java.io.FileOutputStream;
44  import java.io.OutputStreamWriter;
45  import java.nio.charset.Charset;
46  import java.util.Locale;
47  
48  public class UnicodeTest {
49  
50      public static void main(String[] args) throws Exception {
51  
52          String commandLineClassNameSuffix = commandLineClassNameSuffix();
53          String commandLineClassName = "ClassA" + commandLineClassNameSuffix;
54          String manifestClassName;
55          if (hasUnicodeFileSystem()) {
56              manifestClassName = "ClassB" + unicode;
57          } else {
58              manifestClassName = "ClassB" + commandLineClassNameSuffix;
59          }
60  
61          generateSource(commandLineClassName, manifestClassName);
62          generateSource(manifestClassName, commandLineClassName);
63          generateManifest(manifestClassName);
64  
65          System.out.println(commandLineClassName);
66      }
67  
68      private static final String fileSeparator = System.getProperty("file.separator");
69      private static final String osName = System.getProperty("os.name");
70      private static final String defaultEncoding = Charset.defaultCharset().name();
71  
72      // language names taken from java.util.Locale.getDisplayLanguage for the respective language
73      private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
74      private static final String s_chinese = "\u4e2d\u6587";
75      private static final String t_chinese = "\u4e2d\u6587";
76      private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
77      private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
78      private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
79      private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
80      private static final String japanese = "\u65e5\u672c\u8a9e";
81      private static final String korean = "\ud55c\uad6d\uc5b4";
82      private static final String lithuanian = "Lietuvi\u0173";
83      private static final String czech = "\u010de\u0161tina";
84      private static final String turkish = "T\u00fcrk\u00e7e";
85      private static final String spanish = "espa\u00f1ol";
86      private static final String thai = "\u0e44\u0e17\u0e22";
87      private static final String unicode = arabic + s_chinese + t_chinese
88              + russian + hindi + greek + hebrew + japanese + korean
89              + lithuanian + czech + turkish + spanish + thai;
90  
91      private static String commandLineClassNameSuffix() {
92  
93          // Mapping from main platform encodings to language names
94          // for Unix and Windows, respectively. Use empty suffix
95          // for Windows encodings where OEM encoding differs.
96          // Use null if encoding isn't used.
97          String[][] names = {
98              { "UTF-8",          unicode,        ""              },
99              { "windows-1256",   null,           ""              },
100             { "iso-8859-6",     arabic,         null            },
101             { "GBK",            s_chinese,      s_chinese       },
102             { "GB18030",        s_chinese,      s_chinese       },
103             { "GB2312",         s_chinese,      null            },
104             { "x-windows-950",  null,           t_chinese       },
105             { "x-MS950-HKSCS",  null,           t_chinese       },
106             { "x-euc-tw",       t_chinese,      null            },
107             { "Big5",           t_chinese,      null            },
108             { "Big5-HKSCS",     t_chinese,      null            },
109             { "windows-1251",   null,           ""              },
110             { "iso-8859-5",     russian,        null            },
111             { "koi8-r",         russian,        null            },
112             { "windows-1253",   null,           ""              },
113             { "iso-8859-7",     greek,          null            },
114             { "windows-1255",   null,           ""              },
115             { "iso8859-8",      hebrew,         null            },
116             { "windows-31j",    null,           japanese        },
117             { "x-eucJP-Open",   japanese,       null            },
118             { "x-EUC-JP-LINUX", japanese,       null            },
119             { "x-pck",          japanese,       null            },
120             { "x-windows-949",  null,           korean          },
121             { "euc-kr",         korean,         null            },
122             { "windows-1257",   null,           ""              },
123             { "iso-8859-13",    lithuanian,     null            },
124             { "windows-1250",   null,           ""              },
125             { "iso-8859-2",     czech,          null            },
126             { "windows-1254",   null,           ""              },
127             { "iso-8859-9",     turkish,        null            },
128             { "windows-1252",   null,           ""              },
129             { "iso-8859-1",     spanish,        null            },
130             { "iso-8859-15",    spanish,        null            },
131             { "x-windows-874",  null,           thai            },
132             { "tis-620",        thai,           null            },
133         };
134 
135         int column;
136         if (osName.startsWith("Windows")) {
137             column = 2;
138         } else {
139             column = 1;
140         }
141         for (int i = 0; i < names.length; i++) {
142              if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
143                  return names[i][column];
144              }
145          }
146          return "";
147     }
148 
149     private static boolean hasUnicodeFileSystem() {
150         if (osName.startsWith("Windows")) {
151             return ! osName.startsWith("Windows 9") &&
152                    ! osName.equals("Windows Me");
153         } else {
154             return defaultEncoding.equalsIgnoreCase("UTF-8");
155         }
156     }
157 
158     private static void generateSource(String thisClass, String otherClass) throws Exception {
159         String fileName = "UnicodeTest-src" + fileSeparator + thisClass + ".java";
160         OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
161         out.write("public class " + thisClass + " {\n");
162         out.write("    public static void main(String[] args) {\n");
163         out.write("        if (!" + otherClass + "." + otherClass.toLowerCase() + "().equals(\"" + otherClass + "\")) {\n");
164         out.write("            throw new RuntimeException();\n");
165         out.write("        }\n");
166         out.write("    }\n");
167         out.write("    public static String " + thisClass.toLowerCase() + "() {\n");
168         out.write("        return \"" + thisClass + "\";\n");
169         out.write("    }\n");
170         out.write("}\n");
171         out.close();
172     }
173 
174     private static void generateManifest(String mainClass) throws Exception {
175         String fileName = "UnicodeTest-src" + fileSeparator + "MANIFEST.MF";
176         FileOutputStream out = new FileOutputStream(fileName);
177         out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
178         // Header lines are limited to 72 bytes.
179         // The manifest spec doesn't say we have to break at character boundaries,
180         // so we rudely break at byte boundaries.
181         byte[] headerBytes = ("Main-Class: " + mainClass + "\n").getBytes("UTF-8");
182         if (headerBytes.length <= 72) {
183             out.write(headerBytes);
184         } else {
185             out.write(headerBytes, 0, 72);
186             int start = 72;
187             while (headerBytes.length > start) {
188                 out.write((byte) '\n');
189                 out.write((byte) ' ');
190                 int count = Math.min(71, headerBytes.length - start);
191                 out.write(headerBytes, start, count);
192                 start += count;
193             }
194         }
195         out.close();
196     }
197 }